home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / batchjob.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  5.5 KB  |  208 lines

  1. import java.awt.Frame;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.Vector;
  7.  
  8. import com.apple.mrj.MRJOSType;
  9. import com.apple.mrj.MRJFileUtils;
  10.  
  11. public class BatchJob implements Runnable
  12. {
  13.     /**
  14.      * Constructs a new BatchJob
  15.      * @param frame, the frame to use when displaying dialogs.
  16.      * @param batchListener the ActionListener to notify when this job completes successfully.
  17.      * @param rawFiles the array of File objects as they were passed to the application.
  18.      * This may contain folders as well as files.
  19.      * @see #processBatch()
  20.      */
  21.     public BatchJob(Frame frame, ActionListener batchListener, File[] rawFiles)
  22.     {
  23.         this.batchListener    = batchListener;
  24.         this.rawFiles        = rawFiles;    
  25.         this.frame            = frame;    
  26.         isSuccessful        = false;
  27.         isStop                = false;
  28.         progressDialog        = new ProgressDialog(frame, false, new ProgressListener());    
  29.         files                = new Vector();
  30.     }
  31.  
  32.     /**
  33.      * Start processing for the batch job.
  34.      * @param myType the MacOS file type to search for
  35.      * @param myCreator the MacOS creator type to set located files to.
  36.      */
  37.     public void processBatch(MRJOSType myType, MRJOSType myCreator)
  38.     {
  39.         this.myType        = myType;
  40.         this.myCreator    = myCreator;
  41.  
  42.         //Make the progress dialog visible.
  43.         //NOTE: if the progress dialog was modal, this will block!
  44.         //If a modal progress dialog is desired, you can put this call in its own thread.
  45.         progressDialog.setVisible(true);
  46.         
  47.         Thread process = new Thread(this);
  48.         process.start();
  49.     }
  50.  
  51.     /**
  52.      * The main body for the processing associated with this batch job.
  53.      */
  54.     public void run()
  55.     {
  56.         for (int i = 0; i < rawFiles.length; ++i)
  57.         {
  58.             gatherFiles(rawFiles[i]);
  59.         }
  60.         //We are done with pre-flight, now process the files.
  61.         processFiles();
  62.     }
  63.     
  64.     /**
  65.      * Recursive file gathering routine.
  66.      * Recurses down a given file hierarchy gathering
  67.      * all the non-directory files into the files vector.
  68.      */
  69.     protected void gatherFiles(File file)
  70.     {
  71.         if(file != null)
  72.         {
  73.             //If the file is a directory, make sure its path ends in a /
  74.             //(forward slash) character, and recurse.
  75.             if (file.isDirectory())
  76.             {
  77.                 java.lang.String directory = file.getPath();
  78.                 if (!directory.endsWith("/"))
  79.                     directory += "/";
  80.  
  81.                 java.lang.String[] fileList = file.list();
  82.  
  83.                 for (int fileInd = 0; fileInd < fileList.length; fileInd++)
  84.                     this.gatherFiles(new File(directory + fileList[fileInd]));
  85.             }
  86.             //If the file is not a directory, it must be a leaf, so add the
  87.             //file to our vector to be processed later.
  88.             else
  89.             {
  90.                 files.addElement(file);
  91.             }
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * The main logic for processing the post-flight files.
  97.      * Takes care of updating the progress dialog, displaying the
  98.      * results dialog, and notifying the batchListener (if successfull).
  99.      * @see #doForEachFile
  100.      */
  101.     protected void processFiles()
  102.     {
  103.         if (files == null)
  104.             return;
  105.  
  106.         File file;
  107.         int numFiles = files.size();
  108.         int errCount = 0;
  109.         
  110.         if (progressDialog != null)
  111.         {
  112.             progressDialog.setFileCount(numFiles);
  113.             progressDialog.setFileIndex(0);
  114.         }
  115.         
  116.         int i;
  117.         for (i = 0; i < numFiles && !isStop; ++i)
  118.         {
  119.             file = (File)files.elementAt(i);
  120.             if (progressDialog != null)
  121.             {
  122.                 progressDialog.setFileIndex(i + 1);
  123.                 progressDialog.setFileLabel(file.getName());
  124.             }
  125.             
  126.             try
  127.             {
  128.                 doForEachFile(file);
  129.             }
  130.             catch(Exception e)
  131.             {
  132.                 ++errCount;
  133.                 System.err.println(e + " occured for " + file.toString());
  134.             }
  135.             try { Thread.sleep(PROCESS_SLEEP); } catch (InterruptedException exc) {}
  136.         }
  137.         
  138.         isSuccessful = errCount == 0 && !isStop;
  139.         
  140.         files = null;
  141.         
  142.         if (progressDialog != null)
  143.             progressDialog.setVisible(false);
  144.  
  145.         ResultsDialog resultsDialog = new ResultsDialog(frame);
  146.         resultsDialog.setLabel1("Done processing " + i + " of " + numFiles + " file" + (numFiles == 1 ? "" : "s") + ".");
  147.         resultsDialog.setLabel2("There " + (errCount == 1 ? "was" : "were") +  " " + (errCount == 0 ? "no" : "" + errCount) + " error" + (errCount == 1 ? "" : "s") + ".");
  148.         
  149.         if (isStop)
  150.         {
  151.             resultsDialog.setLabel3("Processing was stopped by user.");
  152.         }
  153.         else
  154.         {
  155.             resultsDialog.setLabel3("Gave all '" + myType + "' files a creator of '" + myCreator + "'.");
  156.         }
  157.         
  158.         resultsDialog.setVisible(true);
  159.         
  160.         if(isSuccessful && batchListener != null)
  161.         {
  162.             batchListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "BatchJob Finished"));
  163.         }
  164.     }
  165.     
  166.     /**
  167.      * Gets called on each post-flight file.
  168.      * This is where each file should be processed (in whatever manner
  169.      * intended).  Here we are just changing all files of a given type
  170.      * to have a given creator.
  171.      */
  172.     protected void doForEachFile(File file) throws IOException
  173.     {
  174.         //Filter for the desired type and change the creator.
  175.         MRJOSType fileType = MRJFileUtils.getFileType(file);
  176.         if (fileType.equals(myType))
  177.         {
  178.             MRJFileUtils.setFileCreator(file, myCreator);
  179.         }
  180.     }
  181.     
  182.     /**
  183.      * An ActionListener responsible for listening to the progress
  184.      * dialog for user requests to stop the process.
  185.      */
  186.     public class ProgressListener implements ActionListener
  187.     {
  188.         public void actionPerformed(ActionEvent event)
  189.         {
  190.             isStop = true;
  191.             progressDialog = null;
  192.         }
  193.     }
  194.  
  195.     //The amount of time to give to other threads between
  196.     //each file processed.
  197.     protected static final int PROCESS_SLEEP = 30;        
  198.  
  199.     protected Vector files;
  200.     protected File[] rawFiles;
  201.     protected boolean isSuccessful;
  202.     protected boolean isStop;
  203.     protected ProgressDialog progressDialog;
  204.     protected MRJOSType myType;
  205.     protected MRJOSType myCreator;
  206.     protected Frame frame;
  207.     protected ActionListener batchListener;
  208. }